var socket;
$(function (){
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (!window.WebSocket) {
        alert("当前浏览器不支持WebSocket协议！推荐使用谷歌浏览器。")
        return;
    }
    socket = new WebSocket("ws://localhost:7070/websocket");
    socket.onmessage = function (event){
        var msg = JSON.parse(event.data);
        if (1 == msg.type){
            jQuery.data(document.body,"channelId",msg.channelId);
            return;
        }
        if (2 == msg.type){
            var channelId = msg.channelId;
            if (channelId == jQuery.data(document.body,'channelId')){
                var module = $(".msgBlockOwnerClone").clone();
                module.removeClass("msgBlockOwnerClone").addClass("msgBlockOwner").css({display: "block"});
                module.find(".headPoint").attr("src","res/img" + msg.userHeadImg);
                module.find(".msgBlock_msgInfo .msgPoint").text(msg.msgInfo);
                $("#msgPoint").before(module);
                util.divScroll();
            } else {
                var module = $(".msgBlockFriendClone").clone();
                module.removeClass("msgBlockFriendClone").addClass("msgBlockFriend").css({display: "block"});
                module.find(".headPoint").attr("src","res/img/" + msg.userHeadImg);
                module.find(".msgBlock_channelId").text("ID：" + msg.channelId);
                module.find(".msgBlock_msgPoint .msgPoint").text(msg.msgInfo);
                $("#msgPoint").before(module);
                util.divScroll();
            }
        }
    };

    socket.onopen = function (event){
        console.info("打开WebSocket服务正常，浏览器支持WebSocket！");
        var clientMsgProtocol = {};
        clientMsgProtocol.type = 1;
        clientMsgProtocol.msgInfo = "请求个人信息";
        socket.send(JSON.stringify(clientMsgProtocol));
    }

    socket.onclose = function (event){
        console.info("WebServer 关闭！");
    }

    document.onkeydown = function (e) {
        if (13 == e.keyCode && e.ctrlKey){
            util.send();
        }
    }
});

util = {
    send: function (){
        if (!window.WebSocket) {return;}
        if (socket.readyState == WebSocket.OPEN) {
            var clientMsgProtocol = {};
            clientMsgProtocol.type = 2;
            clientMsgProtocol.msgInfo = $("#sendBox").val();
            socket.send(JSON.stringify(clientMsgProtocol));
            $("#sendBox").val("");
        }else{
            alert("WebSocket 连接没有建立成功！");
        }
    },
    divScroll: function (){
        var div = document.getElementById('show');
        div.scrollTop = div.scrollHeight;
    }
}